package com.laiketui.order.mapstruct;

import com.laiketui.order.api.dto.*;
import com.laiketui.order.domain.Order;
import com.laiketui.order.domain.OrderDetail;
import com.laiketui.order.domain.OrderParent;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Mapper
public interface OrderMapstructMapper {
    OrderMapstructMapper INSTANCE = Mappers.getMapper(OrderMapstructMapper.class);



    @Mapping( target = "oldTotalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getOldTotalPrice()))")
    @Mapping( target = "totalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTotalPrice()))")
    @Mapping( target = "taxation",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTaxation()))")
    @Mapping( target = "payPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getPayPrice()))")
    @Mapping( target = "settlementAmount",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getSettlementAmount()))")
    @Mapping( target = "totalCostPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTotalCostPrice()))")
    OrderDTO toDTO(Order order);

    @Mapping( target = "oldTotalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getOldTotalPrice()))")
    @Mapping( target = "totalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTotalPrice()))")
    @Mapping( target = "taxation",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTaxation()))")
    @Mapping( target = "payPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getPayPrice()))")
    @Mapping( target = "settlementAmount",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getSettlementAmount()))")
    @Mapping( target = "totalCostPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTotalCostPrice()))")
    MchOrderDTO toMchDTO(Order order);

    @Mapping( target = "totalPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTotalPrice()))")
    @Mapping( target = "taxation",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTaxation()))")
    @Mapping( target = "payPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getPayPrice()))")
    @Mapping( target = "totalCostPrice",expression="java(com.laiketui.order.utils.PriceUtil.longToBigDecimalPrice(order.getTotalCostPrice()))")
    ExamineOrderDTO toExamineDTO(Order order);

    default List<OrderDTO> toDTOs(List<Order> orderParents){
        if(CollectionUtils.isEmpty(orderParents)){
            return new ArrayList<>();
        }
        return orderParents.stream().map(this::toDTO).collect(Collectors.toList());
    };

    default List<MchOrderDTO> toMchDTOs(List<Order> records){
        if(CollectionUtils.isEmpty(records)){
            return new ArrayList<>();
        }
        return records.stream().map(this::toMchDTO).collect(Collectors.toList());
    };

    default List<ExamineOrderDTO> toExamineDTOs(List<Order> records){
        if(CollectionUtils.isEmpty(records)){
            return new ArrayList<>();
        }
        return records.stream().map(this::toExamineDTO).collect(Collectors.toList());
    }
}
